﻿#include "guild_crystal_logic.h"

#include "GuildCrystal_struct.h"
#include "GuildModuleCrystal_struct.h"

#include "error/make_error_code.h"
#include "guild/module/guild_module.h"
#include "guild/module/guild_module_loader.hpp"
#include "guild/module/guild_module_saver.hpp"

#include "guild/guild_loader.hpp"

#include "guildservicetimer.service.GuildServiceTimer.proxy.h"

namespace guild_service {

bool GuildModuleCrystalLogic::CrystalPray(const role_uuid_type role_uuid, uint32_t crystal_id, error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }

  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceBuildingName, role_uuid,
                                 guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }

  GuildModuleCrystal module_crystal;
  if (!detail::GetGuildModule<GuildModuleCrystal>(
          kGuildServiceBuildingName, guild_id, kGuildModuleCrystalName,
          module_crystal)) {
    ec = ErrorCode::ERR_GUILD_MOD_CRYSTAL_LOAD_FAILED;
    return false;
  }

  // TODO

  if (!detail::SaveGuildModule<GuildModuleCrystal>(kGuildServiceBuildingName,
                                                   module_crystal, guild_id,
                                                   kGuildModuleCrystalName)) {
    ec = ErrorCode::ERR_GUILD_MOD_CRYSTAL_LOAD_FAILED;
    return false;
  }

  return true;
}
  
bool GuildModuleCrystalLogic::CrystalCharge(const role_uuid_type role_uuid, uint32_t crystal_id, error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }

  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceBuildingName, role_uuid,
                                 guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }

  GuildModuleCrystal module_crystal;
  if (!detail::GetGuildModule<GuildModuleCrystal>(
          kGuildServiceBuildingName, guild_id, kGuildModuleCrystalName,
          module_crystal)) {
    ec = ErrorCode::ERR_GUILD_MOD_CRYSTAL_LOAD_FAILED;
    return false;
  }

  // TODO

  if (!detail::SaveGuildModule<GuildModuleCrystal>(kGuildServiceBuildingName,
                                                   module_crystal, guild_id,
                                                   kGuildModuleCrystalName)) {
    ec = ErrorCode::ERR_GUILD_MOD_CRYSTAL_LOAD_FAILED;
    return false;
  }

  return true;
}

}  // guild_service